﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for PersonManager
/// </summary>
public static class PersonManager
{


    public static void deleteUser(string userName)
    {
        Person person = DatabaseManager.getPerson(userName);
        person.Status = Configuration.PersonStatus.deleted.ToString("g");
        DatabaseManager.editPerson(person);
        MembershipUser user = Membership.GetUser(userName);
        user.IsApproved = false;
        Membership.UpdateUser(user);
    }
    public static Person addNewUser(string email, string password, string first_name, string last_name, string academical_degree, string street, string city, string postal_code, string phone)
    {
        Guid activation_code = Guid.NewGuid();
        Person p = null;
        Account a = null;
        string err_text;
        MembershipCreateStatus out_err;
        try
        {
            // nastavit neaktivniho uživatele
            Membership.CreateUser(email, password, email, "otazkaprokontrolu", "odpovedprokontrolu", true /*aktivovan-predelat*/, out out_err);
            Roles.AddUserToRole(email, Configuration.UserRoles.client.ToString("g"));

            a = AccountManager.createAccount(100000);
            p = new Person(email, Configuration.PersonStatus.waiting_for_activation.ToString("g"), first_name, last_name, academical_degree, phone, street, city, postal_code, a.Id, activation_code.ToString());

            if (DatabaseManager.savePerson(ref p))
                return p;
            else
                return null;
        }
        catch (MembershipCreateUserException e)
        {
            err_text = GetErrorMessage(e.StatusCode);
        }
        catch (HttpException e)
        {
            err_text = e.Message;
        }
        catch (Exception e)
        {
        }
        return null;
    }




    private static string GetErrorMessage(MembershipCreateStatus status)
    {   // Z MSDN popis chyby u AddNewUser, vice mene k ničemu
        switch (status)
        {
            case MembershipCreateStatus.DuplicateUserName:
                return "Username already exists. Please enter a different user name.";

            case MembershipCreateStatus.DuplicateEmail:
                return "A username for that e-mail address already exists. Please enter a different e-mail address.";

            case MembershipCreateStatus.InvalidPassword:
                return "The password provided is invalid. Please enter a valid password value.";

            case MembershipCreateStatus.InvalidEmail:
                return "The e-mail address provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidAnswer:
                return "The password retrieval answer provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidQuestion:
                return "The password retrieval question provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidUserName:
                return "The user name provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.ProviderError:
                return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

            case MembershipCreateStatus.UserRejected:
                return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

            default:
                return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
        }
    }

}
